#ifndef _LIB_LOG
#define _LIB_LOG
#include "usolib.lib.js"

var script_log_viewer=<><![CDATA[
<div class="generic_dialog_popup" style="top: 95px; width:570px !important;">
	<table id="pop_dialog_table" class="pop_dialog_table" style="width:570px !important;">
	<tbody>
		<tr>
			<td class="pop_topleft"></td>
			<td class="pop_border pop_top"></td>
			<td class="pop_topright"></td>
		</tr>
		<tr>
			<td class="pop_border pop_side"></td>
			<td id="pop_content" class="pop_content">
				<h2 class="dialog_title"><span id="a_title">Log Viewer</span></h2>
				<div class="dialog_content">
					<div id="a_content" class="dialog_body">
						<h3 class="mwt_pref_title">Filter</h3>
						<center>Bounty Wins <input type="checkbox" id="mwtlv_bounty" /> -
						Fights Won <input type="checkbox" id="mwtlv_fightswon" /> -
						Fights Lost <input type="checkbox" id="mwtlv_fightslost" /> -
						Jobs Done <input type="checkbox" id="mwtlv_jobs" /><br />
						Purchases <input type="checkbox" id="mwtlv_property" /> -
						Hospital <input type="checkbox" id="mwtlv_hospital" /> -
						Bank <input type="checkbox" id="mwtlv_bank" /> -
						Level <input type="checkbox" id="mwtlv_level" /></center>
						<h3 class="mwt_pref_title">Result</h3>
						<textarea readonly="readonly" id="mwtlv_log" style="width:520px;height:250px;"></textarea>
					</div>
					<div class="dialog_buttons">
						<input id="mwtlv_close" value="Close" name="cancel" class="inputsubmit inputaux" type="button">
					</div>
				</div>
			</td>
			<td class="pop_border pop_side"></td>
		</tr>
		<tr>
			<td class="pop_bottomleft"></td>
			<td class="pop_border pop_bottom"></td>
			<td class="pop_bottomright"></td>
		</tr>
	</tbody>
	</table>
</div>
]]></>.toString();

var Log = {
	memory: [],
	show: function() {
		var div = document.createElement('div');
		div.setAttribute('style', 'z-index:100 !important;');
		div.id = "mwt_log_viewer";
		div.className = "generic_dialog pop_dialog";
		div.innerHTML = script_log_viewer;
		document.body.insertBefore(div, document.body.lastChild);
		$('#mwtlv_close').bind('click', function() {
			Log.hide();
		});
		$("//div[@id='mwt_log_viewer']//input").bind('change', function() {
			Log.update();
		});
		Log.update();
		return;
	},
	hide: function() {
		$('#mwt_log_viewer').animate({
			opacity: "0"
		},
		100, function() {
			$(this).remove();
		});
	},
	update: function(filter) {
		var array = Log.memory,
		filters = filter || [],
		filter = [];
		$("//div[@id='mwt_log_viewer']//input").each(function() {
			filter = [];
			if (this.checked) {
				var name = this.id.match(new RegExp('mwtlv_(.*)'))[1];
				switch (name) {
				case 'bounty':
					filter.push({
						name:
						'Script',
						match: 'Hitlist'
					});
					filter.push({
						name: 'Action',
						match: 'Bounty Claimed'
					});
					filters.push(filter);
					break;
				case 'fightswon':
					filter.push({
						name:
						'Action',
						match: 'Fight Won'
					});
					filters.push(filter);
					break;
				case 'fightslost':
					filter.push({
						name:
						'Action',
						match: 'Fight Lost'
					});
					filters.push(filter);
					break;
				case 'jobs':
					filter.push({
						name:
						'Script',
						match: 'Job'
					});
					filter.push({
						name: 'Action',
						match: 'Doing Job'
					});
					filters.push(filter);
					break;
				case 'property':
					filter.push({
						name:
						'Script',
						match: 'Property'
					});
					filter.push({
						name: 'Action',
						match: 'Buying'
					});
					filters.push(filter);
					break;
				case 'hospital':
					filter.push({
						name:
						'Action',
						match: 'Healing'
					});
					filters.push(filter);
					break;
				case 'bank':
					filter.push({
						name:
						'Action',
						match: 'Banking'
					});
					filters.push(filter);
					break;
				case 'level':
					filter.push({
						name:
						'Script',
						match: 'Level'
					});
					filters.push(filter);
					break;
				default:
					break;
				}
			}
		});
		var include = false,
		text = '',
		match = false,
		matches = 0;
		$.each(array, function() {
			details = this;
			include = false;
			if (filters.length > 0) {
				$.each(filters, function(j) {
					filter = this;
					matches = 0;
					$.each(filter, function(k) {
						match = false;
						$.each(details, function(l) {
							if (this.name == filter[k].name) if (this.value.match(filter[k].match)) match = true;
						});
						if (match) matches++;
					});
					if (matches == filter.length) include = true;
				});
			} else include = true;
			if (include) {
				$.each(details, function() {
					if (this.name == "Time")
						text += 'Time: ' + (this.value.getMonth() + 1) + '/' + this.value.getDate() + '/' + this.value.getFullYear() + ' ' + this.value.toLocaleTimeString();
					else
						text += this.name + ": " + this.value + "; ";
				});
				text += "\n";
			}
		});
		$('#mwtlv_log')[0].value = text;
	}
};
#endif
